package ws.very.common.web.socket

import scala.util.Try
import org.json4s.JValue
import ws.very.util.json.JsonHelper
import org.json4s.JsonDSL._
import com.joyrec.util.log.impl.slf4j.ClassSlf4jDebugLog
@deprecated
object JsonCmd {
  def unapply(jsonStr: S) = Try {
    parseJson(jsonStr)
  }.toOption

}
@deprecated
class ReqJsonCmd[R](cmd: S, getField: JValue => R) extends ClassSlf4jDebugLog {
  import JsonOps._
  private def isCmd(jVal: JValue, cmd: S) =
    get[S](jVal, "p0") == cmd

  def unapply(jobj: JValue) =
    try {
      if (isCmd(jobj, cmd))
        Some(debugVal(s"$jobj parse result") { getField(jobj) })
      else None
    } catch {
      case e: Exception =>
        error(s"$jobj parse error", e)
        None
    }

}

/*
 * {
		"h":请求体,
		"r":响应体, //视各具体指令
		"e":错误体,//目前没有
		"s":200//status码 200为正常
	}
 */
@deprecated
case class ResJson[H, R, E](head: JValue = null, ok: JValue = null, error: JValue = null, status: I = 200) {
  def toJsonStr =
    toJson(("h" -> Option(head)) ~
      ("r" -> Option(ok)) ~
      ("e" -> Option(error)) ~
      ("s" -> status))

}

